
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .banner {
            width: 200px;
            height: 300px;
            margin: 40px auto;
            overflow: hidden;
            position: relative;
        }

        .banner ul {
            width: 1000px;
            height: 300px;
            display: flex;
            position: absolute;
            background-color: #f00;
        }

        .banner ul li {
            width: 200px;
            height: 300px;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 50px;
        }

        span {
            position: absolute;
            z-index: 3;
        }

        ol {
            position: absolute;
            width: 100%;
            bottom: 10px;
            display: flex;
            justify-content: center;
        }

        ol li {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background-color: #0f0;
            margin: 10px;
        }

        span {
            position: absolute;
            top: 0;
            bottom: 0;
            margin: auto;
            width: 20px;
            height: 20px;
            background-color: rgba(0, 0, 0, .5);
            color: #fff;
            text-align: center;
            line-height: 20px;
        }

        #next {
            right: 0;
        }
    </style>
</head>

<body>

    <div class="banner">
        <span id="prev">&lt;</span>
        <span id="next">&gt;</span>
        <ul id="ul">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>
        <ol>
            <li style="background: yellow;"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ol>
    </div>

    <script>

        // 获取对象
        var oBanner = document.querySelector('.banner');
        var oPrev = oBanner.querySelector('#prev');
        var oNext = oBanner.querySelector('#next');
        var oUl = oBanner.querySelector('ul');
        var oDots = oBanner.querySelectorAll('ol li');

        // 轮播的功能
        //   自动播放   ---  显示下一张图片
        //   上翻页     ---  显示上一张图片
        //   下翻页    ---   显示下一张图片
        //   小点切换   ---   点哪个点，就显示哪张图片

        //   结论 ---- 显示第几张图片

        var index = 0;   // 显示第几张图片

        var t;  // 定时器




        // 自动播放  --- 每隔1s播放下一张
        autoPlay()



        // 上翻页
        oPrev.onclick = function () {
            clearInterval(t);
            index--;
            if (index === -1) {
                index = oDots.length - 1;
            }
            show()
            autoPlay();
        }

         // 下翻页
         oNext.onclick = function () {
            clearInterval(t);
            index++;
            if (index === oDots.length) {
                index = 0;
            }
            show()
            autoPlay();
        }


        // 小点
        // 循环事件
        for(var i = 0; i < oDots.length ; i++) {
            // 绑定自定义属性
            oDots[i].index = i ;
            oDots[i].onclick = function() {
                clearInterval(t) ;
                // 
                index = this.index ;
                show() ;
                autoPlay()
            }
        }



        function show() {
            // 显示对应那一张图片
            oUl.style.left = -index * 200 + 'px';
            for (var i = 0; i < oDots.length; i++) {
                oDots[i].style.background = '#0f0';
            }
            oDots[index].style.background = 'yellow';
        }


        function autoPlay() {
            t = setInterval(function () {
                index++;
                if (index === oDots.length) {
                    index = 0;
                }
                show()
            }, 1000)
        }



    </script>

</body>

</html>